const fs = require('fs');

const template = fs.readFileSync('dist/client/index.html', 'utf-8');
const render = require('./dist/server/server-entry').render;
//↓所有需要渲染的路由的名字
const routesToRender = fs.readdirSync('src/pages').map(file => {
  return file.replace('.jsx', '').toLowerCase(); //和我们的App.jsx里 onst routes = Object.keys(pages).map(...)中设置的route path相对应
});

for (const route of routesToRender) {
  const context = {};
  render(route, context)
    .then(html => {
      const responseHtml = template.replace('<!--APP_HTML-->', html);
      const filePath = `dist/static/${route}.html`;
      // 原生fs的模块写入 需要先创建对应的目录
      fs.writeFileSync(filePath, responseHtml);
    });
}
